<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/21
 * Time: 17:03
 */
namespace app\api\controller;
use think\Db;
use think\Request;
class My extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }
    }
    //banner图
    public function banner(){
        extract(input());
        $res = Db::table('up_banner')
            ->where('banner_cate','in','1,2')
            ->where('banner_status',1)
            ->field('banner_id,banner_thumb,banner_type,banner_link')
            ->order('banner_sort desc')
            ->select();
        return json(['status'=>200,'msg'=>$res]);
    }
    //banner图详情
    public function bannerDetail(){
        extract(input());
        $id = input('param.id');
        $banner_content = Db::table('up_banner')->where('banner_id',$id)->value('banner_content');
        return json(['status'=>200,'msg'=>$banner_content]);
    }
    //企业简介
    public function qyjj(){
        $agreement = Db::table('up_other')->where('other_id', 9)->value('other_qyjj');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //企业简介
    public function qyjs(){
        $agreement = Db::table('up_other')->where('other_id', 10)->value('other_qyjs');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //投放广告
    public function tfgg(){
        $agreement = Db::table('up_other')->where('other_id', 8)->value('other_tfgg');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }

    //合伙人
    public function hhr(){
        $agreement = Db::table('up_other')->where('other_id', 7)->value('other_hhr');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //申请成为合伙人
    public function franchisee(){
        extract(input());
        $name = input('param.name');
        $phone = input('param.phone');
        $city = input('param.city');
        $data['franchisee_name'] = $name;
        $data['franchisee_phone'] = $phone;
        $data['franchisee_city'] = $city;
        $data['franchisee_time'] = date('Y-m-d H:i:s',time());
        Db::table('up_franchisee')->insertGetId($data);
        return json(['status' => 200, 'msg' => "申请成功"]);
    }
    //加盟商地图打点
    public function address(){
       $res = Db::table('up_u_user')->where('user_role',2)->field('user_match_lon,user_match_lat')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //个人资料
    public function userList(){
        extract(input());
        $res = Db::table('up_u_user')->where('user_id',$user_id)
            ->field('user_image,user_username,user_gongsi,user_grjj,user_yyzz')
            ->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //编辑个人资料
    public function updateUser(){
        extract(input());
        $user_image = input('param.user_image');
        $user_username = input('param.user_username');
        $user_gongsi = input('param.user_gongsi');
        $user_grjj = input('param.user_grjj');
        $user_yyzz = input('param.user_yyzz');
        if($user_yyzz!=''){
            $result1 =$this->yyzz($user_yyzz) ;
            if($result1['error_code']=='0'){
                if(strlen($result1['result']['res'][0]['value'])!=18){
                    return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
                }
            }else{
                return json(['status' => 0, 'msg' =>"营业执照上传有误,请上传清晰图片"]);
            }

            $data['user_yyzz'] =$user_yyzz;
            $data['user_gongsi'] = $result1['result']['res'][8]['value'];
//            $data['user_company_detail'] = $result1['result']['res'][7]['value'];
//            $data['user_jms_address'] = $result1['result']['res'][7]['value'];
        }
        $data['user_image'] =$user_image;
        $data['user_username'] =$user_username;
        $data['user_gongsi'] =$user_gongsi;
        $data['user_grjj'] =$user_grjj;
        Db::table('up_u_user')->where('user_id',$user_id)->update($data);
        return json(['status' => 200, 'msg' => "更新成功"]);
    }
    //版本介绍
    public function bbjs(){
        $agreement = Db::table('up_other')->where('other_id', 3)->value('other_bbjs');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //法律声明
    public function flsm(){
        $agreement = Db::table('up_other')->where('other_id', 4)->value('other_flsm');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //平台协议
    public function ptxy(){
        $agreement = Db::table('up_other')->where('other_id', 5)->value('other_ptxy');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //隐私协议
    public function ysxy(){
        $agreement = Db::table('up_other')->where('other_id', 5)->value('other_ptxy');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //功能介绍
    public function gnjs(){
        $agreement = Db::table('up_other')->where('other_id', 6)->value('other_gnjs');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    //常见问题
    public function problem(){
        extract(input());
        $res = Db::table('up_problem')->field('problem_id,problem_title')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //常见问题详情
    public function problemDetail(){
        extract(input());
        $problem_id = input('param.problem_id');
        $res = Db::table('up_problem')->where('problem_id',$problem_id)->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    //意见反馈类型
    public function feedbackType(){
        extract(input());
        $where['feedback_type_status'] = 1;
        $where['is_del'] = 1;
        $res = Db::table('up_feedback_type')->field('feedback_type_id,feedback_type_name')->where($where)->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //意见反馈
    public function feedback(){
        extract(input());
        $feedback_type_id = input('param.feedback_type_id');
        $feedback_img = input('param.feedback_img');
        $feedback_content = input('param.feedback_content');
        $data['feedback_type_id'] = $feedback_type_id;
        $data['f_user_id'] = $user_id;
        $data['feedback_img'] = $feedback_img;
        $data['feedback_content'] = $feedback_content;
        $data['feedback_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_feedback')->insertGetId($data);
        if($res){
            return json(['status'=>200,'msg'=>"反馈成功"]);
        }else{
            return json(['status'=>0,'msg'=>'反馈失败']);
        }
    }

    //加盟商合同
    public function contract(){
        extract(input());
        $time = input('param.contract_date');
        if(strlen($time)<2){
            $time = '0'.$time;
        }
        $info = Db::table('up_u_user')->where('user_id',$user_id)->field('f_user_id')->find();
        if($info['f_user_id']==''){
            return json(['status'=>0,'msg'=>'加盟商不能为空']);
        }

        $contract_img = input('param.contract_img');
        $contract_status = input('param.contract_status');
        if($time>28){
            return json(['status'=>0,'msg'=>'选择日期不能大于28']);
        }

        $data['q_user_id'] = $user_id;
        $data['j_user_id'] = $info['f_user_id'];
        $data['contract_img'] = $contract_img;
        $data['contract_status'] = $contract_status;
        $data['contract_date'] = $time;
        $data['contract_time'] = date('Y-m-d H:i:s',time());
        //查询企业和加盟商的合同是否存在
        $where['q_user_id'] = $user_id;
        $where['j_user_id'] = $info['f_user_id'];
        $list = Db::table('up_contract')->where($where)->find();
        if($list['contract_id']==''){//合同不存在插入数据
           $res = Db::table('up_contract')->insertGetId($data);
        }else{
            $res =Db::table('up_contract')->where($where)->update($data);
        }

        if($res){
            return json(['status'=>200,'msg'=>'提交成功']);
        }else{
            return json(['status'=>0,'msg'=>"提交失败"]);
        }
    }

    public function contract1(){
        extract(input());
        //查询企业和加盟商的合同是否存在
        $where['q_user_id'] = $user_id;
        $list = Db::table('up_contract')->where($where)->find();
        if($list['contract_id']==''){
            return json(['status'=>100,'msg'=>""]);
        }else{
            return json(['status'=>200,'msg'=>$list]);
        }
    }

    public function echarts(){
        $where['is_del'] = 1;
        $where['is_status'] = 1;
        $res = Db::table('up_car_cx')->where($where)->field('cx_name')->select();
        foreach ($res as $key=>&$vo){
            $cx_name = $vo['cx_name'] ;
            $map['cx_id'] = array('like',"%$cx_name%");
            $num = Db::table('up_order')->where($map)->count();
            $vo['num'] = $num;
        }
        return json(['status'=>200,'msg'=>$res]);
    }
}